#This is an example of making pie charts # the first plot comes from data given on the # the background page for the associated video # We want to get the values that we will plot # into our script. Use the c() function to # create our variable holding the values fn_length <- c( 7, 47, 93, 117, 95, 57, 24, 8, 4) # then, in its most basic form we can get a plot pie( fn_length ) # that is a bit smaller than we want so try this pie(fn_length, radius=1) # And we can just expand the area of the lower # right pane in RStudio pie(fn_length, radius=1) # of course, it would be nice to get labels on the # bars. To do this we will assign names to the # values in our variable fn_length names(fn_length) <- c("Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "over ten") # and then create a new plot pie( fn_length, radius=1 ) # *********************************************** # *** This is as fancy as you need to get *** # *** for any bar plot reuired in this *** # *** class. The rest is just showing off. *** # *********************************************** # Now I want to extend the vertical scale, To do this # we mmake our command a bit more complex pie( fn_length, radius=1, col=rainbow(11)) # And we should put a title on this graph pie( fn_length, radius=1, col=rainbow(11), main="Length of First Names for Math 160 Students") # and, we could change the direction of the slices pie( fn_length, radius=1, col=rainbow(11), clockwise=TRUE, init.angle=0, main="Length of First Names for Math 160 Students") ######################################################### # Now look at the second problem ######################################################### # Load gnrnd4 so that we can generate the values source( "../gnrnd4.R") # Then run gnrnd4 to actually create the values gnrnd4(745099103, 600031) # The values are in L1, inspect them to be sure # that we have the right values L1 # # We might think that we can just use the pie() # functon on L1 but that will not work pie(L1) # we have a slicw for each value in the data # and the angle for each slice in the pie # is the relative # magnitude of the associated value. # but what we want is a pie chart that shows the frequency # of each different value in the data. We could try to # count the number of 35's, the number of 36's, and # so on, but we will almost certainly make at least one # mistake. So, let the computer do it for us. The # function table() will get those frequencies... table(L1) # So I see that there are 8 "32's", 21 "33's, and # so on. Now, I have the same kind of information # that we had in the first problem. We could do the # same steps and create a variable that holds the # frequencies and then plot that variable. But there # is a shorter way to do this, just give barplot() # the result of the table() function. pie( table(L1) ) # And, we can fancy that up to our heart's content pie( table(L1), radius=1, main="Frequencies of values in the table", col=c("red","green","blue","orange","purple", "tan"))